package cn.web03.web.admin;

import cn.web03.NotFoundException;
import cn.web03.po.Resource;
import cn.web03.service.ResourceService;
import cn.web03.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

@Controller
public class FileController {

    @Autowired
    private UploadService uploadService;

    @RequestMapping(value = "/upload/{type}",method = RequestMethod.POST)
    @ResponseBody
    public Object uploadImage(@RequestParam("file") MultipartFile file, @PathVariable String type, HttpServletRequest request, RedirectAttributes attributes, Model model) {
        String url = null;
        if ("image".equals(type)) {
            url = uploadService.uploadImage(file);
        } else if ("file".equals(type)) {
            url = uploadService.uploadFile(file);
        } else {
            attributes.addFlashAttribute("uploadMessage", "上传失败");
        }
        if (url == null) {
            attributes.addFlashAttribute("uploadMessage", "上传失败");
        }
        model.addAttribute("uploadUrl", url);
        return url;
    }

}
